package com.yuqian.talkservers;


import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.yuqian.Resposoyory.OffOnlineMessageReponsitory;
import com.yuqian.bean.Message;
import com.yuqian.bean.OffOnlineMessage;
import com.yuqian.bean.TalkingUser;
import com.yuqian.bean.User;
import com.yuqian.tools.SpringContextUtil;

import java.util.TreeMap;

/**
 * Created by Administrator on 2017/7/27 0027.
 *
 * 本类用于用户上下线的管理，用户消息发送的管理，以及文件，图片等消息发送的管理
 */

public class UserUtils {
    private static TreeMap<String,TalkingUser> portList=new TreeMap<>();
    private static TreeMap<String,TalkingUser> userList=new TreeMap<>();
    static Gson gson=new Gson();

    public static void sendToAll(String message){
        out("发送的信息为"+message);
        for (String key:userList.keySet()){
            TalkingUser user=userList.get(key);
            if (user.isDevice1Online()){
                out("用户id为"+user.getUserId());
                try{
                    user.getDevice1().println(message);
                out("另一个版本"+gson.toJson(message));
                    out("另一个版本2"+gson.toJson(message).toString().replaceAll("\\\\",""));
                }catch (Exception e){
                    user.setDevice1Online(false);
                    System.out.println("user "+ user.getUserId()+"device1 is office online" );
                }

                if (user.isDevice2Online()){
                    try{
                        user.getDevice2().println(gson.toJson(message).toString());

                    }catch (Exception e){
                        user.setDevice2Online(false);
                    }

                }

            }
        }

    }

    public static boolean hasUser(User user){
        //out(user.getId());
        if (userList.containsKey(String.valueOf(user.getId()))){
            return true;
        }else {
            return false;
        }

    }
    public static void out(String info){
        System.out.println(info);
    }
    public static TalkingUser getUser(String userId){
        return userList.get(userId);
    }

    public static boolean sendMessage(String messagetext){
    try{


        Message message= gson.fromJson(messagetext,Message.class);
        if (message.getMessageType().equals("group")){
           return GroupUtils.sendMessage(message);
        }else if (message.getMessageType().equals("user")){
            if (!userList.containsKey(message.getTowhere())){
                out("数组中不包含此用户");

              //  com.yuqian.tools.OffOnlineMessage.addOffOnlineMessage(messagetext);

              OffOnlineMessageReponsitory reponsitory= (OffOnlineMessageReponsitory) SpringContextUtil.getBean("Resposoyory.OffOnlineMessageReponsitory");
              System.out.println(reponsitory==null?"空":"not null");
                reponsitory.save( gson.fromJson(messagetext, OffOnlineMessage.class));
            }else {
            TalkingUser user= userList.get(message.getTowhere());

            if (user.isDevice1Online()){

                try{
                 user.getDevice1().println(gson.toJson(message).toString());

                }catch (Exception e){

                    user.setDevice1Online(false);
                }

                if (user.isDevice2Online()){
                    try{
                        user.getDevice2().write(gson.toJson(message).toString());
                        user.getDevice2().flush();
                    }catch (Exception e){
                        user.setDevice2Online(false);
                    }

                }
                return true;
            }else {

            }
            }
        }


    }catch (Exception e){
        System.out.println("发送失败"+e);
        //此处编写保存到离线消息列表的方法

    }
        return false;
    }
    /**
     * 用户状态变更通知啊
     * @param user
     */
    public static void userOnLine(TalkingUser user,String port){

            if (userList.containsKey(user.getUserId())){

            }else {
            userList.put(user.getUserId(),user);
            portList.put(port,user);
            }
            GroupUtils.getIsOnlineMessage(user);

    }
    public static String offOnlineMessage(String port){
        String message=null;//
        if (portList.containsKey(port)){
            TalkingUser user=portList.get(port);

            if (user.getDevice1Port().equals(port)){

                message="用户:"+user.getUserId()+"的设备1已断开连接";
                user.setDevice1Online(false);
                if (!user.isDevice1Online()&&!user.isDevice2Online()){

                    userList.remove(user.getUserId());
                }
            }else if (user.getDevice2Port().equals(port)){
                message="用户:"+user.getUserId()+"的设备2已断开连接";
                user.setDevice2Online(false);
                if (!user.isDevice1Online()&&!user.isDevice2Online()){

                    userList.remove(user.getUserId());
                }
            }
        }else {
            message="用户设备已移除";
        }
        removePortWithUser(port);
        return message;
    }

    public static void  removePortWithUser(String port){
        portList.remove(port);

    }
    public static int getOnlineNum(){
        return userList.size();
    }







}
